package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFCheckboxWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFCheckboxWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFCheckboxWidget widget = AnnotatedElementUtils.findMergedAnnotation(field, SFCheckboxWidget.class);
        generate(node, widget);
    }

    private void generate(UIForm node, SFCheckboxWidget widget) {
        ObjectNode ui = node.getUi();

        ui.put("widget", "checkbox");
        putInt(ui, "span", widget.span());
        ui.put("styleType", widget.styleType().type());
        ui.put("checkAll", widget.checkAll());
        putString(ui, "checkAllText", widget.checkAllText());
    }

    @Override
    public Class[] supports() {
        return new Class[]{SFCheckboxWidget.class};
    }
}
